package action.auth;

import org.apache.struts2.ServletActionContext;

import service.interf.security.IAuthority;
import action.common.MyBaseAction;

import com.opensymphony.xwork2.ModelDriven;

import dao.pojo.Authority;

public class AuthorityAction extends MyBaseAction<Authority> implements
		ModelDriven<Authority> {
	private static final long serialVersionUID = 1L;
	private IAuthority iAuthority;
	private String userName;

	/************************* setter and getter *******************************/
	public void setiAuthority(IAuthority iAuthority) {
		this.iAuthority = iAuthority;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Authority getModel() {
		return model;
	}

	// constructor
	public AuthorityAction() {
		super(Authority.class);
		validRules.clear();
		validRules.put("authority", "empty|maxLength(50)");
		validRules.put("userName", "empty|maxLength(50)");
		validRules.put("datas", "empty");
	}

	/**************************** method ***************************************/

	/**
	 * 添加权限
	 */
	public void add() {
		if (!validMe("authority"))
			return;
		String r = "1";
		try {
			iAuthority.addRecord(model);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}
	
	/**
	 * 删除权限
	 */
	public void del() {
		if (!validMe("authority"))
			return;
		String r = "1";
		try {
			iAuthority.delRecord("Authority", model);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}
	
	/**
	 * 删除权限
	 */
	public void delIds() {
		if (!validMe("ids"))
			return;
		String r = "1";
		try {
			iAuthority.delByIds("Authority", "authority", model.getIds());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}
	
	

	/**
	 * 批量保存
	 */
	public void batch() {
		if (!validMe("datas"))
			return;
		String r = "1";
		try {
			iAuthority.batchSave("Authority", model.getDatas(),null);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 获取所有权限
	 */
	public void findAll() {
		String r;
		try {
			r = iAuthority.findByProperty("Authority", "*", "json", true, null,
					null, null, null, model.getPage(), model.getRows());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), r);
	}

}
